
import moment from "moment";

//深拷贝
export  const clone = (any)=> {
	if (checkType(any) === 'Object') { // 拷贝对象
		const o = {};
		for (const key in any) {
			o[key] = clone(any[key])
		}
		return o;
	} else if (checkType(any) === 'Array') { // 拷贝数组
		const arr = []
		for (let i = 0, leng = any.length; i < leng; i++) {
			arr[i] = clone(any[i])
		}
		return arr;
	} else if (checkType(any) === 'Function') { // 拷贝函数
		return new Function('return ' + any.toString()).call(this)
	} else if (checkType(any) === 'Date') { // 拷贝⽇期
		return new Date(any.valueOf())
	} else if (checkType(any) === 'RegExp') { // 拷贝正则
		return new RegExp(any)
	} else if (checkType(any) === 'Map') { // 拷贝Map 集合
		const m = new Map()
		any.forEach((v, k) => {
			m.set(k, clone(v))
		})
		return m
	} else if (checkType(any) === 'Set') { // 拷贝Set 集合
		const s = new Set()
		for (const val of any.values()) {
			s.add(clone(val))
		}
		return s
	}
	return any;
}
//校验类型
function checkType(any) {
	return Object.prototype.toString.call(any).slice(8, -1)
}


export function getCropColor(val) {
	let obj = {
		color: '',
		name: '',
		background: ''
	}
	switch (val) {
		case '0':
			obj.name = '玉米';
			obj.color = '#F5BA93';
			break;
		case '1':
			obj.name = '棉花';
			obj.color = '#F5E193';
			break;
		case '2':
			obj.name = '小麦';
			obj.color = '#93AAF5';
			break;
		case '3':
			obj.name = '甜菜';
			obj.color = '#F5BA93';
			break;
		case '4':
			obj.name = '土豆';
			obj.color = '#93DBF5';
			break;
		case '5':
			obj.name = '水稻';
			obj.color = '#93AAF5';
			break;
		default:
			obj.name = '其它';
			obj.color = '#93F5E1';
			break;
	}
	return obj
}

//根据温度获取颜色
export const getColorByTem = (val) => {
	let fillColor = '';
	if ( val < -10){
		fillColor = "#031b41";
	}
	else if (val >= -10 && val <= -5) {
		fillColor = "#042B66";
	} else if (val > -5 && val <= 0) {
		fillColor = "#064199";
	} else if (val > 0 && val <= 4) {
		fillColor = "#0856CC";
	} else if (val > 4 && val <= 7) {
		fillColor = "#317FF5";
	} else if (val > 7 && val <= 10) {
		fillColor = "#3199F5";
	} else if (val > 10 && val <= 13) {
		fillColor = "#31DBF5";
	} else if (val > 13 && val <= 16) {
		fillColor = "#62F5D7";
	} else if (val > 16 && val <= 19) {
		fillColor = "#62F5A6";
	} else if (val > 19 && val <= 22) {
		fillColor = "#62F576";
	} else if (val > 22 && val <= 25) {
		fillColor = "#E8F531";
	} else if (val > 25 && val <= 28) {
		fillColor = "#F5CE31";
	} else if (val > 28 && val <= 31) {
		fillColor = "#F5AF31";
	} else if (val > 31 && val <= 34) {
		fillColor = "#F59931";
	} else if (val > 37 && val <= 40) {
		fillColor = "#F57231";
	} else if (val > 40 && val <= 43) {
		fillColor = "#CC4908";
	} else {
		fillColor = "#993706";
	}
	return fillColor;
};

//根据气压获取颜色
export const getColorByPressure = (val) => {
	let fillColor = '';
	if (val <= 840) {
		fillColor = "#042B66";
	} else if (val > 840 && val <= 845) {
		fillColor = "#064199";
	} else if (val > 845 && val <= 850) {
		fillColor = "#0856CC";
	} else if (val > 850 && val <= 855) {
		fillColor = "#317FF5";
	} else if (val > 855 && val <= 860) {
		fillColor = "#3199F5";
	} else if (val > 860 && val <= 863) {
		fillColor = "#31DBF5";
	} else if (val > 863 && val <= 866) {
		fillColor = "#62F5D7";
	} else if (val > 866 && val <= 869) {
		fillColor = "#62F5A6";
	} else if (val > 869 && val <= 872) {
		fillColor = "#62F576";
	} else if (val > 872 && val <= 875) {
		fillColor = "#E8F531";
	} else if (val > 875 && val <= 878) {
		fillColor = "#F5CE31";
	} else if (val > 878 && val <= 881) {
		fillColor = "#F5AF31";
	} else if (val > 881 && val <= 884) {
		fillColor = "#F59931";
	} else if (val > 887 && val <= 890) {
		fillColor = "#F57231";
	} else if (val > 890 && val <= 900) {
		fillColor = "#CC4908";
	} else {
		fillColor = "#993706";
	}
	return fillColor;
};

//根据风速获取颜色
export const getColorByWindSpeed = (val) => {
	let fillColor = '';
	if (val <= 0) {
		fillColor = "#042B66";
	} else if (val > 0 && val <= 3) {
		fillColor = "#064199";
	} else if (val > 3 && val <= 6) {
		fillColor = "#0856CC";
	} else if (val > 6 && val <= 9) {
		fillColor = "#317FF5";
	} else if (val > 9 && val <= 12) {
		fillColor = "#3199F5";
	} else if (val > 12 && val <= 15) {
		fillColor = "#31DBF5";
	} else if (val > 15 && val <= 18) {
		fillColor = "#62F5D7";
	} else if (val > 18 && val <= 21) {
		fillColor = "#62F5A6";
	} else if (val > 21 && val <= 24) {
		fillColor = "#62F576";
	} else if (val > 24 && val <= 27) {
		fillColor = "#E8F531";
	} else if (val > 27 && val <= 30) {
		fillColor = "#F5CE31";
	} else if (val > 30 && val <= 33) {
		fillColor = "#F5AF31";
	} else if (val > 33 && val <= 37) {
		fillColor = "#F59931";
	} else if (val > 37 && val <= 40) {
		fillColor = "#F57231";
	} else if (val > 40 && val <= 45) {
		fillColor = "#CC4908";
	} else {
		fillColor = "#993706";
	}
	return fillColor;
};

//根据降水获取颜色
export const getColorByPrecip = (val) => {
	let fillColor = '';
	if (val <= 0) {
		fillColor = "#993706";
	} else if (val > 0 && val <= 5) {
		fillColor = "#CC4908";
	} else if (val > 5 && val <= 10) {
		fillColor = "#F57231";
	} else if (val > 10 && val <= 15) {
		fillColor = "#F59931";
	} else if (val > 15 && val <= 20) {
		fillColor = "#F5AF31";
	} else if (val > 20 && val <= 25) {
		fillColor = "#F5CE31";
	} else if (val > 25 && val <= 30) {
		fillColor = "#E8F531";
	} else if (val > 30 && val <= 35) {
		fillColor = "#62F576";
	} else if (val > 35 && val <= 40) {
		fillColor = "#62F5A6";
	} else if (val > 40 && val <= 45) {
		fillColor = "#62F5D7";
	} else if (val > 45 && val <= 50) {
		fillColor = "#31DBF5";
	} else if (val > 50 && val <= 55) {
		fillColor = "#3199F5";
	} else if (val > 55 && val <= 60) {
		fillColor = "#317FF5";
	} else if (val > 60 && val <= 65) {
		fillColor = "#0856CC";
	} else if (val > 65 && val <= 70) {
		fillColor = "#064199";
	} else {
		fillColor = "#042B66";
	}
	return fillColor;
};

//根据土壤温度获取颜色
export const getColorBySoilTem = (val) => {
	let fillColor = '';
	if (val >= -10 && val <= -5) {
		fillColor = "#042B66";
	} else if (val > -5 && val <= 0) {
		fillColor = "#064199";
	} else if (val > 0 && val <= 4) {
		fillColor = "#0856CC";
	} else if (val > 4 && val <= 7) {
		fillColor = "#317FF5";
	} else if (val > 7 && val <= 10) {
		fillColor = "#3199F5";
	} else if (val > 10 && val <= 13) {
		fillColor = "#31DBF5";
	} else if (val > 13 && val <= 16) {
		fillColor = "#62F5D7";
	} else if (val > 16 && val <= 19) {
		fillColor = "#62F5A6";
	} else if (val > 19 && val <= 22) {
		fillColor = "#62F576";
	} else if (val > 22 && val <= 25) {
		fillColor = "#E8F531";
	} else if (val > 25 && val <= 28) {
		fillColor = "#F5CE31";
	} else if (val > 28 && val <= 31) {
		fillColor = "#F5AF31";
	} else if (val > 31 && val <= 34) {
		fillColor = "#F59931";
	} else if (val > 37 && val <= 40) {
		fillColor = "#F57231";
	} else if (val > 40 && val <= 43) {
		fillColor = "#CC4908";
	} else {
		fillColor = "#993706";
	}
	return fillColor;
};

//根据土壤温度获取颜色
export const getColorBySoilHumidity = (val) => {
	let fillColor = '';
	if (val <= 30) {
		fillColor = "#993706";
	} else if (val > 30 && val <= 35) {
		fillColor = "#CC4908";
	} else if (val > 35 && val <= 40) {
		fillColor = "#F57231";
	} else if (val > 40 && val <= 45) {
		fillColor = "#F59931";
	} else if (val > 45 && val <= 50) {
		fillColor = "#F5AF31";
	} else if (val > 50 && val <= 55) {
		fillColor = "#F5CE31";
	} else if (val > 55 && val <= 60) {
		fillColor = "#E8F531";
	} else if (val > 60 && val <= 65) {
		fillColor = "#62F576";
	} else if (val > 65 && val <= 70) {
		fillColor = "#62F5A6";
	} else if (val > 70 && val <= 75) {
		fillColor = "#62F5D7";
	} else if (val > 75 && val <= 80) {
		fillColor = "#31DBF5";
	} else if (val > 80 && val <= 85) {
		fillColor = "#3199F5";
	} else if (val > 85 && val <= 90) {
		fillColor = "#317FF5";
	} else if (val > 90 && val <= 95) {
		fillColor = "#0856CC";
	} else if (val > 95 && val <= 100) {
		fillColor = "#064199";
	} else {
		fillColor = "#042B66";
	}
	return fillColor;
};

//获取当前年份的指定前index年的年份数组 index默认为0时只取当前年份
export const getYears = (index = 0) => {
	let years = [];
	let year = moment().format("YYYY");
	let startTime = moment().subtract(index - 1, "years");
	years.push({ date: startTime.format("YYYY"), actived: false });
	for (let i = 1; i < index; i++) {
		let day = moment(startTime).add(i, "years").format("YYYY");
		years.push({ date: day, actived: day == year ? true : false });
	}
	return years;
};

//获取当前日期的指定前后index天的日期数组 index默认为0时只取当天的日期
export const getDates = (index = 0) => {
	let dates = [];
	let today = moment().format("MM.DD");
	let startTime = moment().subtract(index, "days");
	dates.push({ date: startTime.format("MM.DD"), actived: false });
	for (let i = 1; i < index * 2 + 1; i++) {
		let day = moment(startTime).add(i, "days").format("MM.DD");
		dates.push({ date: day, actived: day == today ? true : false });
	}
	return dates;
};

/**
 * 根据一个日期计算出其所在的一月，反正对应一月的相应日期
 * basisDate: 依据日期, 默认是当天
 * replenishWeek: 是否需要将该月第一天和最后一天所在的周补全（通日历类似），默认需要
 */
export const calcMonthDate = (basisDate = moment().format("YYYY-MM"), replenishWeek = false, endDate = null) => {
	let monthDate = [];
	let monthEnd = null
	const monthStart = moment(basisDate).startOf("month").format("YYYY-MM-DD"); // 该月第一天
	if (endDate && (basisDate == moment(endDate).format("YYYY-MM"))) {
		monthEnd = endDate; // 指定最后一天
	} else {
		monthEnd = moment(basisDate).endOf("month").format("YYYY-MM-DD"); // 该月最后一天
	}

	monthDate.push(monthStart); // 先将当月第一天放到数组中
	let addNum = 1;
	while (monthDate[monthDate.length - 1] < monthEnd) {
		monthDate.push(
			moment(monthStart).add(addNum, "days").format("YYYY-MM-DD")
		);
		addNum++;
	}

	if (replenishWeek) {
		// 根据参数，补全该月第一天和最后一天所在的周
		// 调用方法，得出该月第一天和最后一天分别是周几 => 星期日为 0、星期六为 6
		const monthStartWeek = moment(monthDate[0]).day();
		const monthEndWeek = moment(monthDate[monthDate.length - 1]).day();
		let addEmpty = 1,
			minusNum = 1;

		/** 注意：我们默认的日历显示是 周一 => 周日 */
		let unshiftEnd = monthStartWeek; // 默认，当月第一天是周几，就往前加n-1天(如周五：往前加5-1天； 周日：往前加7-1天)
		if (monthStartWeek === 0) {
			unshiftEnd = 7;
		}

		while (minusNum < unshiftEnd) {
			// 如果是周一，则一天也不会加
			// 一次减1；即分别在第一天的基础上减一天、两天、三天....;然后放到数组的前面
			monthDate.unshift(
				moment(monthStart).subtract(minusNum, "days").format("YYYY-MM-DD")
			);
			minusNum++;
		}

		if (monthEndWeek !== 0) {
			// 如果最后一天是周日，则不补齐剩余日期
			// 7 - monthEndWeek 即这个月最后一天后面要加的天数（一周是7天） =>> 往后把一周补全(最后以周六结束，周日在最前面)
			// while(addEmpty < (7 - monthEndWeek + 1)) {   // 7 - monthEndWeek + 1 : 周天在最后；7 - monthEndWeek：周天在最前
			while (addEmpty <= 7 - monthEndWeek) {
				//<= : 周天在最后；<：周天在最前
				monthDate.push(
					moment(monthEnd).add(addEmpty, "days").format("YYYY-MM-DD")
				);
				addEmpty++;
			}
		}
	}
	return monthDate;
};


/**
 * 计算一个点是否在多边形里
 * @param {Object} pt 标注点
 * @param {Object} poly 多边形数组
 */
export const isInsidePolygon = (pt, poly) => {
	// eslint-disable-next-line no-var
	for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
		((poly[i].lat <= pt.lat && pt.lat < poly[j].lat) || (poly[j].lat <= pt.lat && pt.lat < poly[i].lat)) &&
			(pt.lng < (poly[j].lng - poly[i].lng) * (pt.lat - poly[i].lat) / (poly[j].lat - poly[i].lat) + poly[i].lng) &&
			(c = !c);
	return c;
}

//取多边形坐标中心点
export const calculateCenter = (coordinates) => {
	let total = coordinates.length;
	let X = 0, Y = 0, Z = 0;
	coordinates.forEach(function (lnglat) {
		let lng = lnglat.lng * Math.PI / 180;
		let lat = lnglat.lat * Math.PI / 180;
		let x, y, z;
		x = Math.cos(lat) * Math.cos(lng);
		y = Math.cos(lat) * Math.sin(lng);
		z = Math.sin(lat);
		X += x;
		Y += y;
		Z += z;
	});
	X = X / total;
	Y = Y / total;
	Z = Z / total;

	let Lng = Math.atan2(Y, X);
	let Hyp = Math.sqrt(X * X + Y * Y);
	let Lat = Math.atan2(Z, Hyp);
	return { lng: Lng * 180 / Math.PI, lat: Lat * 180 / Math.PI };
}

//获取两点经纬度距离
export const getDistance = ( lng1,lat1,lng2, lat2 ) => {
	lat1 = lat1 || 0;
	lng1 = lng1 || 0;
	lat2 = lat2 || 0;
	lng2 = lng2 || 0;
	let rad1 = lat1 * Math.PI / 180.0;
	let rad2 = lat2 * Math.PI / 180.0;
	let a = rad1 - rad2;
	let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
	let r = 6378137;
	let distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));
	return (distance / 1000).toFixed(2);
}

//根据矩形两点坐标pt1,pt2 左下角和右上角 获取间隔为n公里的经纬度坐标
export const calculateCoordinates = (pt1,pt2,n) => {
	let width = parseFloat(getDistance(pt1.lng,pt1.lat,pt2.lng,pt1.lat))
	let height = parseFloat(getDistance(pt1.lng,pt1.lat,pt1.lng,pt2.lat))
	let xCoefficient = (pt2.lng-pt1.lng)/(width/n)
	let yCoefficient = (pt2.lat-pt1.lat)/(height/n)
	let arr = []
	for(let i = pt1.lng;i< pt2.lng;i+= xCoefficient){
		for(let j = pt1.lat;j< pt2.lat;j+= yCoefficient){
			arr.push({lng:i,lat:j})
		}
	}
	return arr
}
